/**
 *
 * @returns 递归函数格式化树状菜单
 */
export function formatTree(data, id = 'id', pid = 'parentId', child = 'children', root = '0') {
  const tree = []
  if (data && data.length > 0) {
    data.forEach((item) => {
      // 获取顶层菜单，parent_id === 0
      if (item[pid] === root) {
        const children = formatTree(data, id, pid, child, item[id])
        if (children && children.length > 0) {
          item[child] = children
        }
        tree.push(item)
      }
    })
  }
  return tree
}

/**
 *
 * @returns 匹配路由
 */
export function matchRouter(data, menuKey) {
  console.log(111, data)
  const tree = []
  if (data && data.length > 0) {
    data.forEach((item) => {
      const name = `/${item.name}`
      // 获取顶层菜单，parent_id === 0
      if (menuKey.includes(name)) {
        const children = matchRouter(item.children, menuKey)
        if (children && children.length > 0) {
          item['children'] = children
        }
        tree.push(item)
      }
    })
  }
  return tree
}